import utilAxios from '@/util/util-axios'

const mechanismApi = utilAxios.initBaseApi('common', 'mechanism')
const platformApi = utilAxios.initBaseApi('function', 'platform')

const queryParams = []
const pageConfig = {
    limit: 20
}
let extendFormConfig = {}
const tableConfig = {
    id: {
        hidden: false
    },
    mechanismId: {
        type: 'customer'
    },
    platformId: {
        type: 'customer'
    },
    channelType: {
        formOnChange
    }
}
const formConfig = {
    ...tableConfig
}
const formRules = {}

function formOnChange(val, form) {
    extendFormConfig = {}
    if (form['channelType'] === 'HAI_KANG') {
        extendFormConfig.__host = {
            name: '__host',
            title: '请求主机'
        }
        extendFormConfig.__httpsTag = {
            name: '__httpsTag',
            title: 'https标记',
            dictList: [
                { name: '是', val: 'true' },
                { name: '否', val: 'false' },
            ]
        }
    }
    return extendFormConfig
}

async function initMechanismData() {
    const res = await mechanismApi.list({limit: 999})
    const content = res.records
    tableConfig.mechanismId.dictList = content.map(item => {
        return {name: item.name, val: item.id}
    })
}

async function initPlatformData() {
    const res = await platformApi.list({limit: 999})
    const content = res.records
    tableConfig.platformId.dictList = content.map(item => {
        return {name: item.name, val: item.id}
    })
}

export async function init() {
    await initMechanismData()
    await initPlatformData()
    return {
        queryParams,
        tableConfig,
        pageConfig,
        formConfig,
        formRules,
        extendFormConfig,
        extendJsonStr: 'settingJson'
    }
}
